from time import sleep
from select import select
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.fedcm import account
from selenium.webdriver.support.select import Select
import pytest

#数据驱动表，以列表的形式创建
@pytest.fixture(params=[['yonghu1','张三','genderf','pm','000010','a123@163.com'],
                        ['yonghu2','李四','genderf','pm','000010','a231@163.com'],
                        ['yonghu3','王一','genderf','pm','000010','a321@163.com']])
def login_data(request):
    return request.param    #取出每一组的元素

#定义一个函数完成从登录到关闭的所有操作
def test_login(login_data):
    driver = webdriver.Chrome()
    driver.get("http://localhost/ranzhi/www")
    # 定位输入框
    # <input type="text" name="account" id="account" value="" class="form-control" placeholder="请输入用户名">
    driver.find_element(By.ID, "account").send_keys("root")
    # 定位密码框
    # <input type="password" name="password" id="password" value="" class="form-control" placeholder="请输入密码">
    driver.find_element(By.ID, "password").send_keys("123456")
    # 点击登录
    # <button type="submit" id="submit" class="btn btn-primary" data-loading="稍候...">登录</button>
    driver.find_element(By.ID, "submit").click()
    sleep(2)
    # 找到后台管理按钮点击切换到后台管理
    # <li id="s-menu-superadmin" data-order="7" class=""><button data-toggle="tooltip" data-tip-class="s-menu-tooltip" data-placement="right" data-btn-type="menu" class="app-btn s-menu-btn open" title="" data-id="superadmin" data-original-title="后台管理"><i class="icon icon-cog"></i></button></li>
    driver.find_element(By.ID, "s-menu-superadmin").click()
    sleep(2)
    # 切换到<iframe页面
    # <iframe id="iframe-superadmin" name="iframe-superadmin" src="/ranzhi/www/sys/admin/" frameborder="no" allowtransparency="true" scrolling="auto" hidefocus="" style="width: 100%; height: 100%; left: 0px;"></iframe>
    a = driver.find_element(By.ID, "iframe-superadmin")
    driver.switch_to.frame(a)
    # 点击添加成员
    driver.find_element(By.LINK_TEXT, "添加成员").click()
    sleep(2)
    # 填写用户名
    # //*[@id="account"]
    driver.find_element(By.ID, 'account').send_keys(f"{login_data[0]}")
    # driver.find_element(By.XPATH, '//*[@id="account"]').send_keys(f"{login_data['account']}")
    sleep(1)
    # 填写真实姓名
    # //*[@id="realname"]
    driver.find_element(By.ID, 'realname').send_keys(f"{login_data[1]}")
    #   driver.find_element(By.XPATH, '//*[@id="realname"').send_keys(f"{login_data['realname']}")
    sleep(1)
    # 选择性别
    # 男
    # <input type="radio" name="gender" value="m" id="genderm">
    # 女
    # <input type="radio" name="gender" value="f" id="genderf">
    driver.find_element(By.ID,f"{login_data[2]}").click()
    #   driver.find_element(By.ID,f"{login_data['gender']}").click()
    sleep(1)
    # 选择角色
    # <select name="role" id="role" class="form-control">
    #<option value="dev">研发</option>
    #<option value="pm">项目经理</option>
    #<option value="market">市场</option>
    a = driver.find_element(By.ID,"role")
    aa = Select(a)
    aa.select_by_value(f"{login_data[3]}")
    #   aa.select_by_value(f"{login_data['value']}")
    sleep(1)
    # 填写密码
    # <input type="password" name="password1" id="password1" value="" class="form-control" autocomplete="off">
    driver.find_element(By.ID,'password1').send_keys(f"{login_data[4]}")
    #   driver.find_element(By.ID,"password1").send_keys(f"{login_data['password']}")
    sleep(1)
    # 填写重复密码
    # <input type="password" name="password2" id="password2" value="" class="form-control" autocomplete="off">
    driver.find_element(By.ID,"password2").send_keys(f"{login_data[4]}")
    #    driver.find_element(By.ID,"password2").send_keys(f"{login_data['password']}")
    sleep(1)
    # 填写邮箱
    # <input type="text" name="email" id="email" value="" class="form-control" autocomplete="off">
    driver.find_element(By.ID,"email").send_keys(f"{login_data[5]}")
    #    driver.find_element(By.ID,"email").send_keys(f"{login_data['email']}")
    sleep(1)
    #点击保存
    #<button type="submit" id="submit" class="btn btn-primary" data-loading="稍候...">保存</button>
    driver.find_element(By.ID,'submit').click()
    # 点击退出登录
    # <button id="showDesk" type="button" class="fullscreen-btn icon-desktop" data-id="home" data-toggle="tooltip" title="" data-original-title="桌面"></button>
    # 退出到最外层
    driver.switch_to.default_content()
    # <a href="javascript:void(0)" class="sign signout">签退</a>
    driver.find_element(By.LINK_TEXT, "签退").click()
    sleep(2)
